unit QQPlot;

interface

uses DiagramFactory, datacontainer, pganalysistypesunit, apgabstractunit, classes,
   xmlintf, pgseriesunit, chartaxisunit, ifmmemoryunit, math;

type
   QQChart = class(DSChart)
   public
      function CreateChartAxis(Axis : PgAxisType) : APgChartAxis; override;
      function CreateSeries(AOwner : TComponent; PrimaryChartAxis : APgChartAxis; MetaSeries : APgMetaSeries; XIndex : Integer) : APgSeries; override;
      constructor Create(AOwner : TComponent; datacontainer : TasacsDataContainer; _Ds : Integer); reintroduce;
   end; // QQChart


   QQSeries = class(PgSeries)
   protected
      function GetInternalValue(I : Integer; SeriesVariable : PgSeriesVariable; MetaSeries : ApgMetaSeries) : Double; override;
   end; // QQSeries

   QObsAxis = class(PgChartAxis)
   protected
      function GetNumDimensionElms : Integer; override;
      function GetAxisLabel : string; override;
   end; // QObsAxis


   QObsAxisSeries = class(PgSeries)
   protected
      function GetInternalValue(I : Integer; SeriesVariable : PgSeriesVariable; MetaSeries : ApgMetaSeries) : Double; override;
   end; // QObsAxisSeries

implementation


//////////////////////////////////
// QQChart


function QQChart.CreateChartAxis(Axis : PgAxisType) : APgChartAxis;
begin
   case Axis of
      atx:
         Result := QObsAxis.Create(Self as IChart, Axis);
   else
      Result := PgChartAxis.Create(Self as IChart, Axis);
   end; // case
end; // QQChart.CreateChartAxis


function QQChart.CreateSeries(AOwner : TComponent; PrimaryChartAxis : APgChartAxis; MetaSeries : APgMetaSeries; XIndex : Integer) : APgSeries;
begin
   Result := nil;
   case PrimaryChartAxis.axis of
      atX:
         if Assigned(MetaSeries) then
           Result := QQSeries.Create(AOwner, PrimaryChartAxis, MetaSeries, XIndex)
         else
            Result := QObsAxisSeries.Create(AOwner, PrimaryChartAxis, MetaSeries, XIndex);
   end; // case
end; // QQChart.CreateSeries


constructor QQChart.Create(AOwner : TComponent; datacontainer : TasacsDataContainer; _Ds : Integer);
var
   M : APgMetaSeries;
begin
   inherited Create(AOwner, datacontainer, _DS);
   Self.Caption := Caption;
   ShowSeriesLegend := false;
   ShowMeanLegend := false;
//      ChartAxis[atX].ShowIntervalSeries := true;
   PlotType := ptScatterSingle;

   chartaxis[atx].AxisLabelPosition := alpCenter;
   chartaxis[atX].ContinuousGroupData := true;
   chartaxis[atX].ShowMinorTick := true;
   chartaxis[atx].DefaultMin := false;
   chartaxis[atx].MinValue := 0;
//   chartaxis[atx].LnTransformed := true;
   //chartaxis[atX].AxisLabelPosition := alpCenter;
   chartaxis[atX].ShowMajorGrid := false;
   chartaxis[aty].AxisLabel := 'Q(Mod)';
   chartaxis[aty].AxisLabelPosition := alpCenter;
   chartaxis[aty].DefaultAxisLabel := false;
   chartaxis[aty].DefaultMin := false;
   chartaxis[aty].MinValue := 0;
 //  chartaxis[aty].LnTransformed := true;
   // Setup meta series at y axis:
   with ChartAxis[atY].MetaSeries do
   begin
      M := TasacsMetaSeries.Create(ChartAxis[aty], nil, 'Q-Q', 0);
      M.Scatter := true;
      Add(Pointer(M));
      M := TasacsMetaSeries.Create(ChartAxis[aty], nil, 'Diagonal', 1);
      M.Scatter := false;
      M.LinePlot := true;
      Add(Pointer(M));
   end;
end; // QQChart.Create


///////////////////////////////
// QQSeries


function QQSeries.GetInternalValue(I : Integer; SeriesVariable : PgSeriesVariable; MetaSeries : ApgMetaSeries) : Double;
var
   S : Integer;
begin
   Result := nan;
   S := Self.MetaSeries.ChartAxis.MetaSeries.IndexOf(Pointer(Self.MetaSeries));
   with QQChart(PrimaryChartAxis.ChartComp) do
   case SeriesVariable of
      svIntervalLow:
         case S of
            0:
               Result := FloatArray(DataSourceContainer(Datacontainer.DS[DS]).DsData[ord(dsModSorted)])[MinCategory + I];
            1:
            begin
               Result := PrimaryChartAxis.Series.GetValue(I, svIntervalLow);
(*               xrate := PrimaryChartAxis.Series.GetValue(I, svIntervalLow) /
                  FloatArray(DataSourceContainer(DataContainer.DS[DS]).DsData[ord(dsObsSorted)]).MaxVal;
               maxval := min(FloatArray(DataSourceContainer(DataContainer.DS[DS]).DsData[ord(dsObsSorted)]).MaxVal,
                  FloatArray(DataSourceContainer(DataContainer.DS[DS]).DsData[ord(dsModSorted)]).MaxVal);
               Result := maxval * xrate;*)
            end;
         end; // case
   else
      Result := inherited GetInternalValue(I, SeriesVariable, MetaSeries)
   end; // case
end; // QQSeries.GetInternalValue


///////////////////
// QObsAxis


function QObsAxis.GetAxisLabel : string;
begin
   if DefaultAxisLabel then
      Result := 'Q(Obs)'
   else
      Result := inherited GetAxisLabel;
end; // QObsAxis.GetAxisLabel


function QObsAxis.GetNumDimensionElms : Integer;
begin
   with QQChart(ChartComp) do
      Result := FloatArray(DataSourceContainer(DataContainer.DS[DS]).DsData[ord(dsObsSorted)]).Capacity;
end; // QObsAxis.GetNumDimensionElms


////////////////
// QObsAxisSeries


function QObsAxisSeries.GetInternalValue(I : Integer; SeriesVariable : PgSeriesVariable; MetaSeries : ApgMetaSeries) : Double;
begin
   Assert(IsAxisSeries);
   case SeriesVariable of
      svIntervalLow:
      with QQChart(PrimaryChartAxis.ChartComp) do
         Result := FloatArray(DataSourceContainer(DataContainer.DS[DS]).DsData[ord(dsObsSorted)])[I + MinCategory];
   else
      Result := inherited GetInternalValue(I, SeriesVariable, MetaSeries);
   end; // case
end; // QObsAxisSeries.GetInternalValue




end.
